﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Collections;
using System.Transactions;

#region Ambito
namespace Distrackting.DataLayer
{
    public partial class Ambito
    {
        public void setAmbito(Ambito amb)
        {
            this.cod_ambito = amb.cod_ambito;
            this.descricao = amb.descricao;
        }
    }
}
#endregion

#region dataAmbito
namespace Distrackting.DataLayer
{
    public class dataAmbito
    {
        #region Methods
        public static void createAmbito(Ambito amb)
        {
                try
                {
                    dbDataDataContext dbData = new dbDataDataContext();
    
                    amb.cod_ambito = System.Guid.NewGuid();
                    dbData.Ambitos.InsertOnSubmit(amb);
                    dbData.SubmitChanges();
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

        public static Ambito getAmbito(Guid guid)
        {
            try
            {
                if (guid == null) return null;
                dbDataDataContext dbData = new dbDataDataContext();

                Ambito amb = (from p in dbData.Ambitos
                             where p.cod_ambito == guid
                             select p).FirstOrDefault();
                return amb;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static Boolean changeAmbito(Ambito amb)
        {
            try
            {
                if (amb == null)
                    return false;
                else
                {
                    dbDataDataContext dbData = new dbDataDataContext();
                    Ambito res = dbData.Ambitos.FirstOrDefault(pnt => pnt.cod_ambito == amb.cod_ambito);
                    if (res == null) return false;
                    else
                    {
                        res.setAmbito(amb);
                        dbData.SubmitChanges();
                        return true;
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static IQueryable<Ambito> getAmbitos()
        {
            try
            {
                dbDataDataContext dbData = new dbDataDataContext();
                return dbData.Ambitos;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static IQueryable<Ambito> getAmbitos(Expression<Func<Ambito, bool>> whereClause)
        {
            try
            {
                dbDataDataContext dbData = new dbDataDataContext();
                return dbData.Ambitos.Where(whereClause);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static Boolean deleteAmbito(Ambito amb)
        {
            try
            {
                dbDataDataContext dbData = new dbDataDataContext();
                if (dbData.Ambitos.Contains(amb))
                {
                    Ambito res = (from pnt in dbData.Ambitos
                                 where pnt.cod_ambito == amb.cod_ambito
                                 select pnt).FirstOrDefault();
                    if (res == null)
                        return false;
                    else
                    {
                        dbData.Ambitos.DeleteOnSubmit(amb);
                        dbData.SubmitChanges();
                        return true;
                    }
                }
                else return false;
            }
            catch (Exception e)
            {
                throw e;
            }
        }


        #endregion
    }
}
#endregion